package com.lzj.jiangxindaojia;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzj.jiangxindaojia.bean.Common;
import com.lzj.jiangxindaojia.http.HttpAction;
import com.lzj.jiangxindaojia.http.HttpCallBack;
import com.lzj.jiangxindaojia.utils.LogUtils;
import com.lzj.jiangxindaojia.utils.Utils;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import static com.lzj.jiangxindaojia.utils.Utils.showToast;

public class ShareActivity
        extends Activity
        implements HttpCallBack, OnClickListener {
    private String content;
    private String html_url;
    private String img;
    private String labelSize;
    private LinearLayout llmain;
    private TextView qq;
    private TextView qq_zone;
    private RelativeLayout rl1;
    private String title;
    private UMShareListener umShareListener = new UMShareListener() {
        public void onCancel(SHARE_MEDIA paramAnonymousSHARE_MEDIA) {
//            Toast.makeText(ShareActivity.this, paramAnonymousSHARE_MEDIA + " 分享取消了", Toast.LENGTH_LONG).show();
        }

        public void onError(SHARE_MEDIA paramAnonymousSHARE_MEDIA, Throwable paramAnonymousThrowable) {
            showToast(ShareActivity.this, paramAnonymousSHARE_MEDIA + "分享失败啦");
            if (paramAnonymousThrowable != null) {
                LogUtils.log("onError: " + paramAnonymousThrowable.getMessage());
            }
        }

        public void onResult(SHARE_MEDIA paramAnonymousSHARE_MEDIA) {
            LogUtils.log("onResult platform  " + paramAnonymousSHARE_MEDIA);
            showToast(ShareActivity.this, paramAnonymousSHARE_MEDIA + "分享成功啦");
//            ShareActivity.this.requestadd();
        }

        public void onStart(SHARE_MEDIA paramAnonymousSHARE_MEDIA) {
            LogUtils.log("onStart platform  " + paramAnonymousSHARE_MEDIA);
        }
    };
    private String video_id;
    private UMWeb web;
    private TextView wechat;
    private TextView wechat_friend;
    private TextView wy;
    private TextView xlweibo;

    private void requestadd() {
//        HashMap localHashMap = new HashMap();
//        localHashMap.put("user_id", SharePreferenceUtils.getData(this, SharePreferenceUtils.USERINFO, SharePreferenceUtils.USER_ID));
//        localHashMap.put("video_id", this.video_id);
//        NetWorks.getInstance().videoShareNum(localHashMap, this, this, ActionId.FENXIANGNUM);
    }

    private void share(SHARE_MEDIA paramSHARE_MEDIA) {
        LogUtils.log("platform  " + paramSHARE_MEDIA);
        new ShareAction(this).withText(" ").withMedia(this.web).setPlatform(paramSHARE_MEDIA).setCallback(this.umShareListener).share();
    }

    public void loadImg() {
        new Thread() {
            public void run() {
                super.run();
                LogUtils.log("img  " + ShareActivity.this.img);
                ShareActivity.this.web.setTitle(ShareActivity.this.title);
                ShareActivity.this.web.setDescription(ShareActivity.this.content);
                if (TextUtils.isEmpty(ShareActivity.this.img)) {
                    ShareActivity.this.web.setThumb(new UMImage(ShareActivity.this,  R.mipmap.ic_launcher));
                    return;
                }
                if (Utils.checkURL(ShareActivity.this.img)) {
                    Bitmap localBitmap = Bitmap.createScaledBitmap(Utils.getBitmap(ShareActivity.this.img), 120, 120, true);
                    ShareActivity.this.web.setThumb(new UMImage(ShareActivity.this, localBitmap));
                    return;
                }
                ShareActivity.this.web.setThumb(new UMImage(ShareActivity.this, R.mipmap.ic_launcher));
            }
        }.start();
    }

    protected void onActivityResult(int paramInt1, int paramInt2, Intent paramIntent) {
        super.onActivityResult(paramInt1, paramInt2, paramIntent);
        UMShareAPI.get(this).onActivityResult(paramInt1, paramInt2, paramIntent);
    }

    public void onClick(View paramView) {
        switch (paramView.getId()) {
            case R.id.rl1:
                finish();
                break;
            case R.id.wechat:
                share(SHARE_MEDIA.WEIXIN);
                break;
            case R.id.wechat_friend:
                share(SHARE_MEDIA.WEIXIN_CIRCLE);
                break;
            case R.id.qq:
                share(SHARE_MEDIA.QQ);
                break;
            case R.id.qq_zone:
                share(SHARE_MEDIA.QZONE);
                break;
            case R.id.xlweibo:
                share(SHARE_MEDIA.SINA);
                break;
            case R.id.wy:
                Utils.showToast(this, "已复制");
                // 得到剪贴板管理器
                ClipboardManager cmb = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                cmb.setText(html_url);
//      ((ClipboardManager)getSystemService("clipboard")).setText("http://app.mabaotoutiao.com/mommy/pc.html?article_id=1");
                break;
        }

    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.activity_share);
        this.rl1 = ((RelativeLayout) findViewById(R.id.rl1));
        this.wechat = ((TextView) findViewById(R.id.wechat));
        this.wechat_friend = ((TextView) findViewById(R.id.wechat_friend));
        this.qq = ((TextView) findViewById(R.id.qq));
        this.qq_zone = ((TextView) findViewById(R.id.qq_zone));
        this.xlweibo = ((TextView) findViewById(R.id.xlweibo));
        this.wy = ((TextView) findViewById(R.id.wy));
        this.wechat.setOnClickListener(this);
        this.wechat_friend.setOnClickListener(this);
        this.qq.setOnClickListener(this);
        this.qq_zone.setOnClickListener(this);
        this.xlweibo.setOnClickListener(this);
        this.wy.setOnClickListener(this);
        this.rl1.setOnClickListener(this);
        this.html_url = getIntent().getExtras().getString("html_url");
        this.img = getIntent().getExtras().getString("img");
        this.title = getIntent().getExtras().getString("title");
        this.content = getIntent().getExtras().getString("content");
//        this.video_id = getIntent().getExtras().getString("video_id");
        this.web = new UMWeb(this.html_url);
        loadImg();
    }

    Common common;
    @Override
    public <T> void onSuccess(String result, String action) {
        switch (action) {

            case HttpAction.FENGXIANG:
//                common = (Common) result;
                setResult(RESULT_OK);
                break;
        }
    }
}

